<?php

/*
 *
 */

class Model_User_Role extends Model_BaseModel
{
    /**
     * 缓存前缀
     *
     * @var unknown
     */
    private static $tag = 'Model_User_Role';
    private $table = "sys_role";

    function __construct()
    {
        // 选择连接的数据库
        parent::_init('mediation');
    }


    /**获取全部数据
     * @return mixed
     */
    public function getListAll()
    {
        $sql = "SELECT * FROM `sys_role`";
        $data = [];
        return $this->dao->conn()->setTag(self::$tag)->preparedSql($sql, $data)->fetchAll();
    }

    /**
     * @param $role_id
     * @return mixed
     */
    public function getProvisioningByRoleId($role_id)
    {
        $sql = "SELECT provisioning FROM {$this->table} WHERE id=:id";
        $data = [
            ':id' => $role_id
        ];
        return $this->dao->conn()->nocache()->preparedSql($sql, $data)->fetchOne();
    }

    /**
     * @param array $fields
     * @param array $search
     * @param bool $cache
     * @return mixed
     */
    function getAllBySearch($fields = array(), $search = array(), $cache = false)
    {
        $fields = empty($fields) ? '*' : implode(',', $fields);
        $search_str = '';
        if (isset($search['where']) && !empty($search['where'])) {
            $search_str .= 'WHERE ' . implode(' AND ', $search['where']);
        }
        if (isset($search['order']) && $search['order']) {
            $search_str .= ' ORDER BY ' . $search['order'];
        }
        if (isset($search['limit']) && $search['limit']) {
            $search['start'] = isset($search['start']) ? $search['start'] : 0;
            $search_str .= ' LIMIT ' . $search['start'] . ',' . $search['limit'];
        }
        $sql = "SELECT {$fields} FROM {$this->table} {$search_str}";
        $cache ? $this->dao->conn()->setTag(self::$tag) : $this->dao->conn(false)->noCache();
        $res = $this->dao->preparedSql($sql, array())->fetchAll();
        return $res;
    }

    /**
     * @param $id
     * @return mixed
     */
    public function getRoleById($id)
    {
        $sql = "SELECT * FROM {$this->table} WHERE id=:id";
        $data = [
            ':id' => $id
        ];
        return $this->dao->conn()->setTag(self::$tag)->preparedSql($sql, $data)->fetchOne();
    }

    /**
     * @param $id
     * @return mixed
     */
    public function deleteById($id)
    {
        $sql = "DELETE FROM {$this->table} WHERE id=:id";
        $data = [
            ':id' => $id,
        ];
        $res = $this->dao->conn(false)->noCache()->preparedSql($sql, $data)->affectedCount();
        if ($res) {
            $this->dao->clearTag(self::$tag);
        }
        return $res;
    }

    /*
     * 存储数据
     */
    public function insertData($saveData)
    {
        foreach ($saveData as $key => $value) {
            $arrFields[] = '`' . $key . '`';
            $temp = ':' . $key;
            $dataA[$temp] = $value;
            $arrValues[] = $temp;
        }
        $sqlA = 'INSERT INTO `' . $this->table . '` (' . implode(',', $arrFields) . ') VALUES (' . implode(',', $arrValues) . ')';
        $uid = $this->dao->conn(false)->noCache()->preparedSql($sqlA, $dataA)->lastInsertId();
        $this->dao->clearTag(self::$tag);
        return $uid;
    }

    /**
     * @param $updateData
     * @return mixed
     */
    public function updateItemById($updateData)
    {
        $sql = "UPDATE {$this->table} SET title=:title,provisioning=:provisioning,update_time=:update_time WHERE id=:id";
        $data = [
            ':id' => $updateData['id'],
            ':title' => $updateData['title'],
            ':provisioning' => $updateData['provisioning'],
            ':update_time' => time(),
        ];
        $res = $this->dao->conn(false)->noCache()->preparedSql($sql, $data)->affectedCount();

        if ($res) {
            $this->dao->clearTag(self::$tag);
        }
        return $res;
    }


    /**
     * @param $title
     * @return array
     */
    public function getByTitle($title)
    {
        $sql = "SELECT * FROM {$this->table} where title=:title";
        $data = array(
            ':title' => $title,
        );
        $res = $this->dao->conn()->noCache()->preparedSql($sql, $data)->fetchOne();
        return $res;
    }
}
